package arithmetic.encrypt.rsa;

import org.junit.Test;

/**
 * @Auther: luobiao
 * @Description:
 */
public class MyTest {

    public static final String publicKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDEAUewZZNer4heUmo8ZF2fjmv\n" +
            "2fzI/JQLKDBu1tAcvncS2rcl3B39auJWyV2rHgmG8oux8DKASKcKz75MzwlRA4G5\n" +
            "e35ky6c4UfffeK+Mbed/M25ebsH4o6eoBCuCJ2imjDI0g5VMc7rbTaDluMQcAXQ6\n" +
            "5S4p5BcOZqd0UHelNwIDAQAB";

    public static final String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMMQBR7Blk16viF5\n" +
            "SajxkXZ+Oa/Z/Mj8lAsoMG7W0By+dxLatyXcHf1q4lbJXaseCYbyi7HwMoBIpwrP\n" +
            "vkzPCVEDgbl7fmTLpzhR9994r4xt538zbl5uwfijp6gEK4InaKaMMjSDlUxzuttN\n" +
            "oOW4xBwBdDrlLinkFw5mp3RQd6U3AgMBAAECgYB9UfiIJwWip54psVvSr8it4TC8\n" +
            "uwc+NZADjwF25In2PaQQt/X82+tkmdvquBp2yqiCS6fWXSSE6KhxOMDhZg5sH3bH\n" +
            "8mWqQMheHObI4gqAUeTwt862H3DE8Npk7fLgGc41FQkcw+OU5pq7R9khqlkxHgPU\n" +
            "pTN6y3W4mawYSqkOwQJBAO08w5jy15/ORSj9u942oX/DFZ2/+ipDiC3PVpvmI5oQ\n" +
            "hs8uS3MPP1OxbwKqgqHD3EMFzyYEpZTxRNcue51CkVcCQQDSfVzM+CxnndWSrd9U\n" +
            "66I3d0YHuM0WI+317+wJ8LwjVLB/Gd9qX1EOaCzOwDeGAU3ZKmMnyvQ7GstVWvXs\n" +
            "Rb8hAkEA499n6bPo1cElwTfnioxjF/tk+f965bm0RnxPrrOiIswZEUxggIZ2x4c5\n" +
            "IBB0JbmiCma8EMKJ4cTV4PLvHaDhRQJBAK4XOXXdnPxmMoqKfDiEWzp8z9L7KcSy\n" +
            "GtJaiWL/VNvz47Z786jLBQNhbg3iNQoEchq3UCNNXQq1co86GEQ+u4ECQDkhPZsd\n" +
            "ECgflvRWlwtr4rsN9iyY6mFlieiFdFJv8abOsJHXpNQSTWDKMXYpNiWDI+RxVN+a\n" +
            "RjztnK+uUE7p8Ms=";

    /**
     * 测试私钥加密公钥解密
     */
    @Test
    public void test1(){
        String content = "测试字符串";
        System.out.println("原始字符为："+content);
        String encrypt=Base64.encode(SignHelper.privateEncrypt(privateKey, content.getBytes(), EncryptArithmeticEnum.RSA));
        System.out.println("私钥加密后base64字符串为："+encrypt);
        String decrypt = new String(SignHelper.publicDecrypt(publicKey, Base64.decode(encrypt), EncryptArithmeticEnum.RSA));
        System.out.println("公钥解密后字符串为："+decrypt);
    }

    /**
     * 测试公钥加解私钥解密
     */
    @Test
    public void test2(){
        String content = "测试字符串";
        System.out.println("原始字符为："+content);
        String encrypt=Base64.encode(SignHelper.publicEncrypt(publicKey, content.getBytes(), EncryptArithmeticEnum.RSA));
        System.out.println("公钥加密后base64字符串为："+encrypt);
        String decrypt = new String(SignHelper.privateDecrypt(privateKey, Base64.decode(encrypt), EncryptArithmeticEnum.RSA));
        System.out.println("私钥解密后字符串为："+decrypt);
    }

    /**
     * 测试签名和签名校验
     */
    @Test
    public void test3(){
        String content = "cardNo=360313199104043015&name=luobiao&prodId=TC2_TWOVALID&requestNo=dgfdfQdfg123456789001&requestTime=2021-01-30 17:23:56&token=ioxp1Pcy85kEM2tn";
        System.out.println("签名原始字符为："+content);
        String sign=SignHelper.sign(privateKey,content, SignArithmeticEnum.MD5WithRSA, EncryptArithmeticEnum.RSA);
        System.out.println("签名后的结果为："+sign);
        boolean check=SignHelper.doCheck(publicKey, content, sign, SignArithmeticEnum.MD5WithRSA, EncryptArithmeticEnum.RSA);
        System.out.println("验签结果："+check);
    }
}
